﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using Drill.Configuration;
using Drill.TestSupport;
using Drill.TestSupport.TestDomains.SimpleDomain;
using Microsoft.Practices.ServiceLocation;
using NUnit.Framework;

namespace Drill.Integration.CommonServiceLocator
{
	[TestFixture]
	public class ServiceLocatorAdapterBackedByUnityDrillBitTests
    {
		private IDependencyResolver CreateDependencyResolver()
		{
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			DependencyResolverManager.Reset();
			return DependencyResolverManager.Create(config);
		}

		private IServiceLocator CreateServiceLocatorAdapter()
		{
			return new ServiceLocatorAdapter(CreateDependencyResolver());
		}

		[Test]
		public void CanConstruct()
		{
			// Arrange
			IDependencyResolver dependencyResolver = CreateDependencyResolver();

			// Act
			ServiceLocatorAdapter serviceLocator = new ServiceLocatorAdapter(dependencyResolver);

			// Assert
			Assert.That(serviceLocator, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "dependencyResolver", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructWithNullDependencyResolverArgument()
		{
			// Arrange
			IDependencyResolver dependencyResolver = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			// ReSharper disable ObjectCreationAsStatement
			new ServiceLocatorAdapter(dependencyResolver);
			// ReSharper restore ObjectCreationAsStatement
			// ReSharper restore ExpressionIsAlwaysNull

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanGetService()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjA obj = serviceLocator.GetService(typeof (ITestObjA)) as ITestObjA;

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(TestObjA)));
		}

		[Test]
		public void CanGetInstance()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjA obj = serviceLocator.GetInstance(typeof(ITestObjA)) as ITestObjA;

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(TestObjA)));
		}

		[Test]
		public void CanGetNamedInstance()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjB obj = serviceLocator.GetInstance(typeof(ITestObjB), "MyObjB") as ITestObjB;

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(ITestObjB)));
		}

		[Test]
		public void CanGetAllInstances()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			IEnumerable<object> enumerable = serviceLocator.GetAllInstances(typeof(ITestObjB));

			// Assert
			Assert.That(enumerable, Is.Not.Null);
			// ReSharper disable AssignNullToNotNullAttribute
			Assert.That(enumerable.Any(o => o is TestObjB), Is.True);
			// ReSharper restore AssignNullToNotNullAttribute
		}

		[Test]
		public void CanGetInstanceGeneric()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjA obj = serviceLocator.GetInstance<ITestObjA>();

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(TestObjA)));
		}

		[Test]
		public void CanGetNamedInstanceGeneric()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjB obj = serviceLocator.GetInstance<ITestObjB>("MyObjB");

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(ITestObjB)));
		}

		[Test]
		public void CanGetAllInstancesGeneric()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			IEnumerable<ITestObjB> enumerable = serviceLocator.GetAllInstances<ITestObjB>();

			// Assert
			Assert.That(enumerable, Is.Not.Null);
			// ReSharper disable AssignNullToNotNullAttribute
			Assert.That(enumerable.Any(o => o is TestObjB), Is.True);
			// ReSharper restore AssignNullToNotNullAttribute
		}
    }
}
